<?php
// 引入公共文件
include_once('./config/config.php');
// 引入数据库配置文件
include_once('./config/check.php');

$adminid = $_GET['adminid'] ?? 0;

// 数据回写
$sql = "select * from per_admin where id=$adminid";

$admin = find($sql);
// var_dump($admin);
// exit;

if (!$admin) {
  ShowMsg('管理员不存在','adminList.php');
  exit;
}

if ($_POST) {
  $password = isset($_POST['password'])? trim($_POST['password']):'';

  $sql = "select * from per_admin where id=$adminid";
  $oldadmin = find($sql);
  if (!$oldadmin) {
    ShowMsg('管理员不存在','adminList.php');
    exit;
  }

  $data = [];

  // 判断是否修改密码
  if (!empty($password)) {
    // 生成密码盐
    $salt = build_randstr();
    // 密码加密
    $password = md5($password . $salt);
    $data['salt'] = $salt;
    $data['password'] = $password;
  }

  // 判断是否修改头像
  if ($_FILES['avatar']['size'] > 0 && $_FILES['avatar']['error'] == 0) {
    $res = upload('avatar');
    if ($res['code'] == 0) {
      ShowMsg($res['msg']);
      exit;
    }
    $data['avatar'] = $res['data'];
  }

  if (!empty($data)) {
    $result = edit('admin',$data,"`id`=$adminid");
    if (!$result) {
      if ($_FILES['avatar']['size'] > 0 && $_FILES['avatar']['error'] == 0) {
        // 删除新头像
        is_file($data['avatar']) && unlink($data['avatar']);
      }
      ShowMsg('修改失败');
      exit;
    }
    if ($_FILES['avatar']['size'] > 0 && $_FILES['avatar']['error'] == 0) {
      // 删除旧头像
      is_file($oldadmin['avatar']) && unlink($oldadmin['avatar']);
    }
  }

  ShowMsg('编辑成功','adminList.php');
  exit;
}
?>

<!DOCTYPE html>
<html lang="en">
  <head>
  <?php include_once('common/meta.php');?>
  </head>
  <body> 
    
    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">添加管理员</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.html">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                    
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 管理员列表</button>
                  <div class="btn-group">
                  </div>
                </div>

                <div class="well">
                    <div id="myTabContent" class="tab-content">
                      <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">
                            <label>用户名</label>
                            <input type="text" class="input-xlarge" name="username" value="<?php echo $admin['username'];?>" readonly>
                            <span id="tips" style="display: none;"></span>
                            <label>密码</label>
                            <input type="password" class="input-xxlarge" name="password">
                            <label>头像</label>
                            <input type="file" id="avatar"  name="avatar" value="<?php echo $admin['avatar'];?>">
                            <label></label>
                            <img src="<?php echo $admin['avatar'];?>" alt="" id="preview" style="width: 100px;"> 
                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交" />
                        </form>
                      </div>
                  </div>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Delete Confirmation</h3>
                  </div>
                  <div class="modal-body">
                    
                    <p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
                  </div>
                  <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-danger" data-dismiss="modal">Delete</button>
                  </div>
                </div>
                
                <?php include_once('common/footer.php'); ?>
                    
            </div>
        </div>
    </div>
  </body>
</html>
<?php include_once('common/script.php');?>

<script>
  $("#avatar").change(function(e){
    let file = e.target.files[0];
    let src = URL.createObjectURL(file);
   $("#preview").attr('src',src);
  })
</script>

